#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :demo_jd_flow.py
# @Time      :2021/7/29 9:48
# @author    :Harry

'''
@case : 京东流量充值
@step：
1、访问京东首页
2、首页话费充值快捷入口选择流量充值
3、进行流量充值
4、关闭退出浏览器
'''
from selenium import webdriver
import time
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select

driver = webdriver.Chrome()
driver.maximize_window()

# 1、访问京东首页
driver.get('https://www.jd.com')
time.sleep(2)

# 2、首页话费充值快捷入口选择流量充值
# 悬浮选择话费
ac_huafei = ActionChains(driver)
huafei = driver.find_element_by_xpath('//div[@id="J_service"]/div[1]/ul/li[1]/a/span')
ac_huafei.move_to_element(huafei).perform()

# 进入 iframe
frame_outer = driver.find_element_by_xpath('//div[@id="J_service"]/div[2]/div[1]/iframe')
driver.switch_to.frame(frame_outer)

# 悬浮选择流量充值
ac_liuliang = ActionChains(driver)
liuliang = driver.find_element_by_link_text('流量充值')
ac_liuliang.move_to_element(liuliang).perform()

# 3、进行流量充值
frame_inner = driver.find_element_by_xpath('//div[@class="content-iframe"]/iframe[2]')
driver.switch_to.frame(frame_inner)
driver.find_element_by_class_name('input-phone').send_keys('13112345678')  # 输入电话号码
time.sleep(1)       # 输入号码后页面变化，必须强制等待
ele = driver.find_element_by_id('faceSelect')
select = Select(ele)
select.select_by_visible_text('10G')  # 选择流量值
driver.find_element_by_class_name('btn-submit').click()  # 点击【快速充值】按钮


driver.switch_to.default_content()  # 退出到最外层

# 4、关闭退出浏览器
time.sleep(2)
driver.quit()
